package com.wn.service.doctorService;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wn.common.PageParam;
import com.wn.mapper.ConsultationMapper;
import com.wn.mapper.DepartmentMapper;
import com.wn.ov.ConsultationOv;
import com.wn.ov.PatientOv;
import com.wn.ov.UserOv;
import com.wn.pojo.Consultation;
import com.wn.pojo.DepartmentCode;
import com.wn.resopnse.ResponseCode;
import com.wn.resopnse.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class ConsultationService {
    @Autowired
    private ConsultationMapper consultationMapper;

    public ServerResponse getConsultationByCreatedId(Integer id, PageParam<ConsultationOv> pageParam) {
        if (!StringUtils.isEmpty(pageParam.getSuperSearchKeyWord())) {
            //执行高级检索
            //分页
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
            //排序
            for (int i = 0; i < pageParam.getOrderParams().length; i++) {
                PageHelper.orderBy(pageParam.getOrderParams()[i]);
            }

            List<ConsultationOv> ovList = consultationMapper.superSearch(pageParam.getSuperSearchKeyWord(), id);
            PageInfo<ConsultationOv> ovPageInfo = new PageInfo<ConsultationOv>(ovList);
            return new ServerResponse(ResponseCode.SUCCESS, ovPageInfo, "查找成功");

        } else {
            //分页
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
            //排序
            for (int i = 0; i < pageParam.getOrderParams().length; i++) {
                PageHelper.orderBy(pageParam.getOrderParams()[i]);
            }
            List<ConsultationOv> ovList = consultationMapper.getConsultationByCreatedId(id);
            PageInfo<ConsultationOv> ovPageInfo = new PageInfo<ConsultationOv>(ovList);

            return new ServerResponse(ResponseCode.SUCCESS, ovPageInfo, "查找成功");
        }
    }

    public ServerResponse addConsultation(UserOv user, Consultation consultation) {
        consultation.setCreateUserId(user.getId());
        int i = consultationMapper.insertSelective(consultation);
        if (i>0){
            return new ServerResponse(ResponseCode.SUCCESS,"成功");
        }
        return new ServerResponse(ResponseCode.ERROR,"失败");
    }

    public ServerResponse deleteConsultation(Consultation consultation) {
        int i = consultationMapper.deleteByPrimaryKey(consultation.getId());
        if (i>0){
            return new ServerResponse(ResponseCode.SUCCESS,"成功");
        }
        return new ServerResponse(ResponseCode.ERROR,"失败");
    }

    public ServerResponse updateConsultation(Consultation consultation) {
        int i = consultationMapper.updateByPrimaryKeySelective(consultation);
        if (i>0){
            return new ServerResponse(ResponseCode.SUCCESS,"成功");
        }
        return new ServerResponse(ResponseCode.ERROR,"失败");
    }

    public ServerResponse getConsultationByDoctorId(Integer id, PageParam<ConsultationOv> pageParam) {
        if (!StringUtils.isEmpty(pageParam.getSuperSearchKeyWord())) {
            //执行高级检索
            //分页
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
            //排序
            for (int i = 0; i < pageParam.getOrderParams().length; i++) {
                PageHelper.orderBy(pageParam.getOrderParams()[i]);
            }

            List<ConsultationOv> ovList = consultationMapper.superSearchByDoctorId(pageParam.getSuperSearchKeyWord(), id);
            PageInfo<ConsultationOv> ovPageInfo = new PageInfo<ConsultationOv>(ovList);
            return new ServerResponse(ResponseCode.SUCCESS, ovPageInfo, "查找成功");

        } else {
            //分页
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
            //排序
            for (int i = 0; i < pageParam.getOrderParams().length; i++) {
                PageHelper.orderBy(pageParam.getOrderParams()[i]);
            }
            List<ConsultationOv> ovList = consultationMapper.getConsultationByDoctorId(id);

            PageInfo<ConsultationOv> ovPageInfo = new PageInfo<ConsultationOv>(ovList);

            return new ServerResponse(ResponseCode.SUCCESS, ovPageInfo, "查找成功");
        }
    }
}
